<html>
<head>
    <script src="../../OLLoader.js"></script>
    <script type="text/javascript">

    function test_read_exception(t) {
        t.plan(1);
        var xml = document.getElementById("exceptionsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);
        var format = new OpenLayers.Format.WMSCapabilities();
        var obj = format.read(doc);
        t.ok(!!obj.error, "Error reported correctly");
    }

    function test_layers(t) {

        t.plan(25);

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);
        var capability = obj.capability;

        var layers = {};
        for (var i=0, len=capability.layers.length; i<len; i++) {
            if ("name" in capability.layers[i]) {
                layers[ capability.layers[i].name ] = capability.layers[i];
            }
        }

        var rootlayer = capability.layers[ capability.layers.length - 1];

        t.eq(rootlayer.srs,
             {"CRS:84": true},
             "SRS parsed correctly for root layer");
        t.eq(layers["ROADS_RIVERS"].srs,
             {"CRS:84": true, "EPSG:26986": true},
             "Inheritance of SRS handled correctly when adding SRSes");
        t.eq(layers["Temperature"].srs,
             {"CRS:84": true},
             "Inheritance of SRS handled correctly when redeclaring an inherited SRS");
        t.eq(layers["Temperature"].infoFormats, ["text/xml", "text/plain", "text/html"], "infoFormats set correctly on layer");
        var bbox = layers["ROADS_RIVERS"].bbox["EPSG:26986"];
        t.eq(bbox.bbox,
             [189000, 834000, 285000, 962000],
             "Correct bbox from BoundingBox");
        t.eq(bbox.res, {x: 1, y: 1}, "Correct resolution");
        bbox = layers["ROADS_RIVERS"].bbox["CRS:84"];
        t.eq(bbox.bbox,
             [-71.63, 41.75, -70.78, 42.90],
             "Correct bbox from BoundingBox (override)");
        t.eq(bbox.res, {x: 0.01, y: 0.01}, "Correct resolution (override)");

        bbox = layers["ROADS_1M"].bbox["EPSG:26986"];
        t.eq(bbox.bbox,
             [189000, 834000, 285000, 962000],
             "Correctly inherited bbox");
        t.eq(bbox.res, {x: 1, y: 1}, "Correctly inherited resolution");


        var identifiers = layers["ROADS_RIVERS"].identifiers;
        var authorities = layers["ROADS_RIVERS"].authorityURLs;

        t.ok(identifiers, "got identifiers from layer ROADS_RIVERS");
        t.ok("DIF_ID" in identifiers,
             "authority attribute from Identifiers parsed correctly");
        t.eq(identifiers["DIF_ID"],
             "123456",
             "Identifier value parsed correctly");
        t.ok("DIF_ID" in authorities,
             "AuthorityURLs parsed and inherited correctly");
        t.eq(authorities["DIF_ID"],
             "http://gcmd.gsfc.nasa.gov/difguide/whatisadif.html",
             "OnlineResource in AuthorityURLs parsed correctly");

        var featurelist = layers["ROADS_RIVERS"].featureListURL;
        t.ok(featurelist, "layer has FeatureListURL");
        t.eq(featurelist.format,
             "XML",
             "FeatureListURL format parsed correctly");
        t.eq(featurelist.href,
             "http://www.university.edu/data/roads_rivers.gml",
             "FeatureListURL OnlineResource parsed correctly");

        t.eq(layers["Pressure"].queryable,
             true,
             "queryable property inherited correctly");
        t.eq(layers["ozone_image"].queryable,
             false,
             "queryable property has correct default value");
        t.eq(layers["population"].cascaded,
             1,
             "cascaded property parsed correctly");
        t.eq(layers["ozone_image"].fixedWidth,
             512,
             "fixedWidth property correctly parsed");
        t.eq(layers["ozone_image"].fixedHeight,
             256,
             "fixedHeight property correctly parsed");
        t.eq(layers["ozone_image"].opaque,
             true,
             "opaque property parsed correctly");
        t.eq(layers["ozone_image"].noSubsets,
             true,
             "noSubsets property parsed correctly");


    }

    function test_dimensions(t) {

        t.plan(8);

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);
        var capability = obj.capability;

        var layers = {};
        for (var i=0, len=capability.layers.length; i<len; i++) {
            if ("name" in capability.layers[i]) {
                layers[ capability.layers[i].name ] = capability.layers[i];
            }
        }

        var time = layers["Clouds"].dimensions.time;
        t.eq(time["default"], "2000-08-22", "Default time value parsed correctly");
        t.eq(time.values.length, 1, "Currect number of time extent values/periods");
        t.eq(time.values[0], "1999-01-01/2000-08-22/P1D", "Time extent values parsed correctly");

        var elevation = layers["Pressure"].dimensions.elevation;
        t.eq(elevation.units, "CRS:88", "Dimension units parsed correctly");
        t.eq(elevation["default"], "0", "Default elevation value parsed correctly");
        t.eq(elevation.nearestVal, true, "NearestValue parsed correctly");
        t.eq(elevation.multipleVal, false, "Absense of MultipleValues handled correctly");
        t.eq(elevation.values,
             ["0","1000","3000","5000","10000"],
             "Parsing of comma-separated values done correctly");


    }

    function test_contactinfo(t) {
        t.plan(14);

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);
        var service = obj.service;

        var contactinfo = service.contactInformation;
        t.ok(contactinfo, "object contains contactInformation property");

        var personPrimary = contactinfo.personPrimary;
        t.ok(personPrimary, "object contains personPrimary property");

        t.eq(personPrimary.person, "Jeff Smith", "ContactPerson parsed correctly");
        t.eq(personPrimary.organization, "NASA", "ContactOrganization parsed correctly");

        t.eq(contactinfo.position,
             "Computer Scientist",
             "ContactPosition parsed correctly");


        var addr = contactinfo.contactAddress;
        t.ok(addr, "object contains contactAddress property");

        t.eq(addr.type, "postal", "AddressType parsed correctly");
        t.eq(addr.address,
             "NASA Goddard Space Flight Center",
             "Address parsed correctly");
        t.eq(addr.city, "Greenbelt", "City parsed correctly");
        t.eq(addr.stateOrProvince, "MD", "StateOrProvince parsed correctly");
        t.eq(addr.postcode, "20771", "PostCode parsed correctly");
        t.eq(addr.country, "USA", "Country parsed correctly");

        t.eq(contactinfo.phone,
             "+1 301 555-1212",
             "ContactVoiceTelephone parsed correctly");
        t.eq(contactinfo.email,
             "user@host.com",
             "ContactElectronicMailAddress parsed correctly");
    }

    function test_feesAndConstraints(t) {
        t.plan(2);

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);
        var service = obj.service;

        t.ok(! ("fees" in service), "Fees=none handled correctly");
        t.ok(! ("accessConstraints" in service), "AccessConstraints=none handled correctly");
    }

    function test_requests(t) {
        t.plan(6);

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);
        var request = obj.capability.request;

        t.ok(request, "request property exists");
        t.ok("getmap" in request, "got GetMap request");

        t.ok("getfeatureinfo" in request, "got GetFeatureInfo request");
        t.eq(request.getfeatureinfo.formats,
             ["text/xml", "text/plain", "text/html"],
             "GetFeatureInfo formats correctly parsed");

        var exception = obj.capability.exception;
        t.ok(exception, "exception property exists");
        t.eq(exception.formats,
             ["XML", "INIMAGE", "BLANK"],
             "Exception Format parsed");
    }

    function test_ogc(t) {
        t.plan(14);

        /*
         * Set up
         */

        // needed for the minScale/maxScale test, see below
        var dpi = OpenLayers.DOTS_PER_INCH;
        OpenLayers.DOTS_PER_INCH = 90.71;

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);
        var capability = obj.capability;

        /*
         * Test
         */

        var attribution = capability.layers[2].attribution;
        t.eq(attribution.title, "State College University", "attribution title parsed correctly.");
        t.eq(attribution.href, "http://www.university.edu/", "attribution href parsed correctly.")
        t.eq(attribution.logo.href, "http://www.university.edu/icons/logo.gif", "attribution logo url parsed correctly.");
        t.eq(attribution.logo.format, "image/gif", "attribution logo format parsed correctly.");
        t.eq(attribution.logo.width, "100", "attribution logo width parsed correctly.");
        t.eq(attribution.logo.height, "100", "attribution logo height parsed correctly.");

        var keywords = capability.layers[0].keywords;
        t.eq(keywords.length, 3, "layer has 3 keywords.");
        t.eq(keywords[0].value, "road", "1st keyword parsed correctly.");

        var metadataURLs = capability.layers[0].metadataURLs;
        t.eq(metadataURLs.length, 2, "layer has 2 metadata urls.");
        t.eq(metadataURLs[0].type, "FGDC:1998", "type parsed correctly.");
        t.eq(metadataURLs[0].format, "text/plain", "format parsed correctly.");
        t.eq(metadataURLs[0].href, "http://www.university.edu/metadata/roads.txt", "href parsed correctly.");

        /*
        Test minScale and maxScale
        */
        var minScale = 250000;
        var maxScale = 1000;
        t.eq(capability.layers[0].minScale, minScale.toPrecision(16), "layer.minScale is correct");
        t.eq(capability.layers[0].maxScale, maxScale.toPrecision(16), "layer.maxScale is correct");

        /*
         * Tear down
         */

        OpenLayers.DOTS_PER_INCH = dpi;
    }

    function test_WMS13specials(t) {
        t.plan(3);

        var xml = document.getElementById("ogcsample").firstChild.nodeValue;
        var doc = new OpenLayers.Format.XML().read(xml);

        var obj = new OpenLayers.Format.WMSCapabilities().read(doc);

        t.eq(obj.service.layerLimit, 16, "LayerLimit parsed correctly");
        t.eq(obj.service.maxHeight, 2048, "MaxHeight parsed correctly");
        t.eq(obj.service.maxWidth, 2048, "MaxWidth parsed correctly");

    }

    </script>
</head>
<body>

<div id="exceptionsample"><!--
<?xml version='1.0' encoding="UTF-8"?> 
<ServiceExceptionReport version="1.3.0" xmlns="http://www.opengis.net/ogc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.opengis.net/ogc
    http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
    <ServiceException> Plain text message about an error. </ServiceException>
    <ServiceException code="InvalidUpdateSequence"> Another error message, this one with a service
        exception code supplied. </ServiceException>
    <ServiceException>
        <![CDATA[ Error in module <foo.c>, line 42
A message that includes angle brackets in text must be enclosed in a Character Data Section as in this example. All XML-like markup is ignored except for this sequence of three closing characters:
]]>
    </ServiceException>
    <ServiceException>
        <![CDATA[ <Module>foo.c</Module> <Error>An error occurred</Error> <Explanation>Similarly, actual XML can be enclosed in a CDATA section. A generic parser will ignore that XML, but application-specific software may choose to process it.</Explanation> ]]>
    </ServiceException>
</ServiceExceptionReport>
--></div>

<!--
OGC example below taken from
http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xml
Changes:
-removed comments
-corrected typo in FeatureListURL Format XML with double quote
-added MinScaleDenominator and MaxScaleDenominator
-remove whitespace in Dimension tags
-->
<div id="ogcsample"><!--
<?xml version='1.0' encoding="UTF-8"?>
<WMS_Capabilities version="1.3.0" xmlns="http://www.opengis.net/wms"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opengis.net/wms http://schemas.opengis.net/wms/1.3.0/capabilities_1_3_0.xsd">
<Service>
  <Name>WMS</Name>
  <Title>Acme Corp. Map Server</Title>
  <Abstract>Map Server maintained by Acme Corporation.  Contact: webmaster@wmt.acme.com.  High-quality maps showing roadrunner nests and possible ambush locations.</Abstract>

  <KeywordList>
    <Keyword>bird</Keyword>
    <Keyword>roadrunner</Keyword>
    <Keyword>ambush</Keyword>
  </KeywordList>
  <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
   xlink:href="http://hostname/" />


  <ContactInformation>
    <ContactPersonPrimary>
      <ContactPerson>Jeff Smith</ContactPerson>
      <ContactOrganization>NASA</ContactOrganization>
    </ContactPersonPrimary>
    <ContactPosition>Computer Scientist</ContactPosition>

    <ContactAddress>
      <AddressType>postal</AddressType>
      <Address>NASA Goddard Space Flight Center</Address>
      <City>Greenbelt</City>
      <StateOrProvince>MD</StateOrProvince>
      <PostCode>20771</PostCode>

      <Country>USA</Country>
    </ContactAddress>
    <ContactVoiceTelephone>+1 301 555-1212</ContactVoiceTelephone>
    <ContactElectronicMailAddress>user@host.com</ContactElectronicMailAddress>
  </ContactInformation>

  <Fees>none</Fees>

  <AccessConstraints>none</AccessConstraints>
  <LayerLimit>16</LayerLimit>
  <MaxWidth>2048</MaxWidth>
  <MaxHeight>2048</MaxHeight>
</Service>
<Capability>
  <Request>
    <GetCapabilities>

      <Format>text/xml</Format>
      <DCPType>
        <HTTP>
          <Get>
            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
             xlink:type="simple"
             xlink:href="http://hostname/path?" />
          </Get>
          <Post>
            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
             xlink:type="simple"
             xlink:href="http://hostname/path?" />

          </Post>
        </HTTP>
      </DCPType>
    </GetCapabilities>
    <GetMap>
      <Format>image/gif</Format>
      <Format>image/png</Format>
      <Format>image/jpeg</Format>

      <DCPType>
        <HTTP>
          <Get>
            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
             xlink:type="simple"
             xlink:href="http://hostname/path?" />
          </Get>
        </HTTP>
      </DCPType>
    </GetMap>

    <GetFeatureInfo>
      <Format>text/xml</Format>
      <Format>text/plain</Format>
      <Format>text/html</Format>
      <DCPType>
        <HTTP>
          <Get>

            <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
             xlink:type="simple"
             xlink:href="http://hostname/path?" />
          </Get>
        </HTTP>
      </DCPType>
    </GetFeatureInfo>
  </Request>
  <Exception>
    <Format>XML</Format>

    <Format>INIMAGE</Format>
    <Format>BLANK</Format>
  </Exception>
  <Layer>
    <Title>Acme Corp. Map Server</Title>
    <CRS>CRS:84</CRS>

    <AuthorityURL name="DIF_ID">
      <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
       xlink:href="http://gcmd.gsfc.nasa.gov/difguide/whatisadif.html" />
    </AuthorityURL>
    <BoundingBox CRS="CRS:84"
     minx="-1" miny="-1" maxx="1" maxy="1" resx="0.0" resy="0.0"/>
    <Layer>

      <Name>ROADS_RIVERS</Name>
      <Title>Roads and Rivers</Title>

      <CRS>EPSG:26986</CRS>
      <EX_GeographicBoundingBox>
        <westBoundLongitude>-71.63</westBoundLongitude>
        <eastBoundLongitude>-70.78</eastBoundLongitude>
        <southBoundLatitude>41.75</southBoundLatitude>
        <northBoundLatitude>42.90</northBoundLatitude>

      </EX_GeographicBoundingBox>
      <BoundingBox CRS="CRS:84"
       minx="-71.63" miny="41.75" maxx="-70.78" maxy="42.90" resx="0.01" resy="0.01"/>
      <BoundingBox CRS="EPSG:26986"
       minx="189000" miny="834000" maxx="285000" maxy="962000" resx="1" resy="1" />
      <Attribution>
        <Title>State College University</Title>
        <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
         xlink:href="http://www.university.edu/" />

        <LogoURL width="100" height="100">
          <Format>image/gif</Format>
          <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
           xlink:type="simple"
           xlink:href="http://www.university.edu/icons/logo.gif" />
        </LogoURL>
      </Attribution>
      <Identifier authority="DIF_ID">123456</Identifier>
      <FeatureListURL>

        <Format>XML</Format>
        <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"
         xlink:href="http://www.university.edu/data/roads_rivers.gml" />
      </FeatureListURL>
      <Style>
        <Name>USGS</Name>
        <Title>USGS Topo Map Style</Title>
        <Abstract>Features are shown in a style like that used in USGS topographic maps.</Abstract>

        <LegendURL width="72" height="72">
          <Format>image/gif</Format>
          <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
           xlink:type="simple"
           xlink:href="http://www.university.edu/legends/usgs.gif" />
        </LegendURL>
        <StyleSheetURL>
          <Format>text/xsl</Format>

          <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
           xlink:type="simple"
           xlink:href="http://www.university.edu/stylesheets/usgs.xsl" />
        </StyleSheetURL>
      </Style>
      <MinScaleDenominator>1000</MinScaleDenominator>
      <MaxScaleDenominator>250000</MaxScaleDenominator>
      <Layer queryable="1">
        <Name>ROADS_1M</Name>
        <Title>Roads at 1:1M scale</Title>
        <Abstract>Roads at a scale of 1 to 1 million.</Abstract>

        <KeywordList>
          <Keyword>road</Keyword>
          <Keyword>transportation</Keyword>
          <Keyword>atlas</Keyword>
        </KeywordList>
        <Identifier authority="DIF_ID">123456</Identifier>
        <MetadataURL type="FGDC:1998">

                <Format>text/plain</Format>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
                 xlink:type="simple"
                 xlink:href="http://www.university.edu/metadata/roads.txt" />
             </MetadataURL>
        <MetadataURL type="ISO19115:2003">
               <Format>text/xml</Format>
               <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
                xlink:type="simple"
                xlink:href="http://www.university.edu/metadata/roads.xml" />
             </MetadataURL>

        <Style>
          <Name>ATLAS</Name>
          <Title>Road atlas style</Title>
          <Abstract>Roads are shown in a style like that used in a commercial road atlas.</Abstract>
        <LegendURL width="72" height="72">
          <Format>image/gif</Format>
          <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink"
           xlink:type="simple"
           xlink:href="http://www.university.edu/legends/atlas.gif" />

        </LegendURL>
        </Style>
      </Layer>
      <Layer queryable="1">
        <Name>RIVERS_1M</Name>
        <Title>Rivers at 1:1M scale</Title>
        <Abstract>Rivers at a scale of 1 to 1 million.</Abstract>

        <KeywordList>
          <Keyword>river</Keyword>
          <Keyword>canal</Keyword>
          <Keyword>waterway</Keyword>
        </KeywordList>
      </Layer>
    </Layer>

    <Layer queryable="1">
      <Title>Weather Forecast Data</Title>
      <CRS>CRS:84</CRS>

      <EX_GeographicBoundingBox>
        <westBoundLongitude>-180</westBoundLongitude>
        <eastBoundLongitude>180</eastBoundLongitude>

        <southBoundLatitude>-90</southBoundLatitude>
        <northBoundLatitude>90</northBoundLatitude>
      </EX_GeographicBoundingBox>
      <Dimension name="time" units="ISO8601" default="2000-08-22">1999-01-01/2000-08-22/P1D</Dimension>
      <Layer>

        <Name>Clouds</Name>
        <Title>Forecast cloud cover</Title>
      </Layer>
      <Layer>
        <Name>Temperature</Name>
        <Title>Forecast temperature</Title>
      </Layer>

      <Layer>
        <Name>Pressure</Name>
        <Title>Forecast barometric pressure</Title>
         <Dimension name="elevation" units="EPSG:5030" />
         <Dimension name="time" units="ISO8601" default="2000-08-22">
           1999-01-01/2000-08-22/P1D</Dimension>

         <Dimension name="elevation" units="CRS:88" default="0" nearestValue="1">0,1000,3000,5000,10000</Dimension>
      </Layer>
    </Layer>
    <Layer opaque="1" noSubsets="1" fixedWidth="512" fixedHeight="256">
      <Name>ozone_image</Name>
      <Title>Global ozone distribution (1992)</Title>

      <EX_GeographicBoundingBox>
        <westBoundLongitude>-180</westBoundLongitude>
        <eastBoundLongitude>180</eastBoundLongitude>
        <southBoundLatitude>-90</southBoundLatitude>
        <northBoundLatitude>90</northBoundLatitude>
      </EX_GeographicBoundingBox>
      <Dimension name="time" units="ISO8601" default="1992">1992</Dimension>

    </Layer>
    <Layer cascaded="1">
      <Name>population</Name>
      <Title>World population, annual</Title>
      <EX_GeographicBoundingBox>
        <westBoundLongitude>-180</westBoundLongitude>

        <eastBoundLongitude>180</eastBoundLongitude>
        <southBoundLatitude>-90</southBoundLatitude>
        <northBoundLatitude>90</northBoundLatitude>
      </EX_GeographicBoundingBox>
      <Dimension name="time" units="ISO8601" default="2000">1990/2000/P1Y</Dimension>
    </Layer>
  </Layer>

</Capability>
</WMS_Capabilities>
--></div>

</body>
</html>
